package com.gemme.gemme.view;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.gemme.gemme.R;
import com.gemme.gemme.login.GlideCircleTransform;
import com.gemme.gemme.util.StrUtil;

import java.util.List;

public class ChatView extends RelativeLayout {
    private Context context;
    Button button1;
    private ChatBean.DataBean.ListBeanX listBeanX;
    private OnLongClickLIstener onLongClickLIstener;
    private OnChildClickLIstener onChildClickLIstener;
    private int praiseFlag;

    public ChatView(Context context) {
        this(context,null);
    }

    public ChatView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public ChatView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        initView();
    }
    ImageView iv_zan;
    ImageView iv_head;

    TextView tv_zan;
    TextView tv_name;
    TextView tv_time;
    TextView tv_desc;
    LinearLayout ll_content;
    boolean isZan = false;
    private void initView() {
        LayoutInflater.from(context).inflate(R.layout.chat_view,this);

        iv_zan = findViewById(R.id.iv_zan);
        tv_zan = findViewById(R.id.tv_zan);

        iv_head = findViewById(R.id.iv_head);
        tv_name = findViewById(R.id.tv_name);
        tv_time = findViewById(R.id.tv_time);
        tv_desc = findViewById(R.id.tv_desc);
        ll_content = findViewById(R.id.ll_content);

/*
        tv_desc.setOnLongClickListener(new OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                return false;
            }
        });
*/
    }

    public void setText(String text){
        button1.setText(text);
    }

    public void setZan(int zan){
        if(zan > 0){
            tv_zan.setText(tv_zan + "");
            iv_zan.setImageResource(R.drawable.pc_dianzan_select);
        }
    }

    public void setData(ChatBean.DataBean.ListBeanX listBeanX) {
        ll_content.removeAllViews();
        this.listBeanX = listBeanX;
        try {
            tv_name.setText(listBeanX.getUserName());
            tv_time.setText(StrUtil.getTime(listBeanX.getCreateTime()));
            tv_desc.setText(listBeanX.getComments());

            List<ChatBean.DataBean.ListBeanX.ChatChildList.ListBean> list = listBeanX.getList().getRecords();
            if(list != null && list.size() > 0){
                for (int i = 0; i < list.size(); i++) {
                    ChatBean.DataBean.ListBeanX.ChatChildList.ListBean listBean = list.get(i);
                    SinChatView sinChatView = new SinChatView(context);
                    sinChatView.setData(listBean);
                    ll_content.addView(sinChatView);
                    //回复子评论
                    setListener(sinChatView);
                }
            }

//.transform(new GlideCircleTransform(context))
            int likeCounts = listBeanX.getLikeCounts();
            setLike(likeCounts,listBeanX.getPraiseFlag());
            String picture = listBeanX.getPicture();
            if(!TextUtils.isEmpty(picture)){
                Glide.with(context).load(picture)
                        .transform(new GlideCircleTransform(context))
                        .into(iv_head);

            }
        }catch (Exception e){

        }
    }

    private void setListener(SinChatView sinChatView) {
        try {
            sinChatView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    ChatBean.DataBean.ListBeanX.ChatChildList.ListBean data = sinChatView.getData();
                    int contentId = data.getContentId();
                    String toUserName = data.getUserName();
                    Integer toUserId = data.getUserId();
                    long pcommentsId = data.getPcommentsId();
                    if(onChildClickLIstener != null){
                        onChildClickLIstener.onChildClickLIstener(contentId,toUserName,toUserId,pcommentsId);
                    }
                }
            });

            //长按删除监听
            sinChatView.setOnLongClickListener(new OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    ChatBean.DataBean.ListBeanX.ChatChildList.ListBean data = sinChatView.getData();
                    int contentId = data.getContentId();
                    String toUserName = data.getUserName();
                    Integer userId = data.getUserId();
                    long pcommentsId = data.getPcommentsId();
                    if(onLongClickLIstener != null){
                        onLongClickLIstener.OnLongClickLIstener(contentId,data.getId(),pcommentsId,userId);
                    }
                    return false;
                }
            });
        }catch (Exception e){

        }
    }

    private void setLike(int likeCounts,int praiseFlag) {
        this.praiseFlag = praiseFlag;
        try {
            if(likeCounts > 0){
                tv_zan.setText(likeCounts + "");
            }else{
                tv_zan.setText("赞");
            }


            if(praiseFlag == 1){ //1代表我点过得赞
                iv_zan.setImageResource(R.drawable.pc_dianzan_select);
                tv_zan.setTextColor(Color.parseColor("#ff9d2c2f"));
            }else{
                iv_zan.setImageResource(R.drawable.pc_dianzan);
                tv_zan.setTextColor(Color.parseColor("#ffaaaaaa"));
            }

           /* if(likeCounts > 0){
                iv_zan.setImageResource(R.drawable.pc_dianzan_select);
                tv_zan.setText(likeCounts);
                tv_zan.setTextColor(Color.parseColor("#9D2C2FFF"));
            }else{
                iv_zan.setImageResource(R.drawable.pc_dianzan);
                tv_zan.setText("赞");
                tv_zan.setTextColor(Color.parseColor("#ffaaaaaa"));
            }*/

        }catch (Exception e){

        }
    }



    public ChatBean.DataBean.ListBeanX getData(){
        return listBeanX;
    }

    public void setOnLongClickLIstener(OnLongClickLIstener onLongClickLIstener){

        this.onLongClickLIstener = onLongClickLIstener;
    }

    public TextView getTv_desc() {
        return tv_desc;
    }

    public ImageView getIv_zan() {
        return iv_zan;
    }



    public interface OnLongClickLIstener{
        void OnLongClickLIstener(Integer contentId,Long id,Long pcommentsId,Integer userId);
    }

    public void setOnChildClickLIstener(OnChildClickLIstener onChildClickLIstener){

        this.onChildClickLIstener = onChildClickLIstener;
    }

    public interface OnChildClickLIstener{
     /*   int contentId = data.getContentId();
        String toUserName = data.getUserName();
        Integer toUserId = data.getUserId();
        long pcommentsId = data.getId();*/

        void onChildClickLIstener(Integer contentId,String toUserName,Integer toUserId,long pcommentsId);
    }


    public boolean getIsZan(){
        return isZan;
    }
    public void setIsZan(boolean isZan) {
        this.isZan = isZan;
    }

    public int getPraiseFlag() {
        return praiseFlag;
    }
}
